package com.ruoyi.web.controller.teacher;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BaseSignalanalysisInfo;
import com.ruoyi.domain.BasisCaseLoadingTask;
import com.ruoyi.domain.BasisUavTable;
import com.ruoyi.service.IBaseSignalanalysisInfoService;
import com.ruoyi.service.IBasisCaseLoadingTaskService;
import com.ruoyi.service.IBasisUavTableService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 案例关联的任务Controller
 *
 */
@RestController
@RequestMapping("/task")
public class BasisCaseLoadingTaskController extends BaseController
{
    @Autowired
    private IBasisCaseLoadingTaskService basisCaseLoadingTaskService;


    @Autowired
    private IBasisUavTableService basisUavTableService;

    @Autowired
    private IBaseSignalanalysisInfoService baseSignalanalysisInfoService;



    /**
     * 查询案例关联的任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BasisCaseLoadingTask basisCaseLoadingTask)
    {
        startPage();
        List<BasisCaseLoadingTask> list = basisCaseLoadingTaskService.selectBasisCaseLoadingTaskList(basisCaseLoadingTask);
        return getDataTable(list);
    }

    /**
     * 导出案例关联的任务列表
     */
    @Log(title = "案例关联的任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasisCaseLoadingTask basisCaseLoadingTask)
    {
        List<BasisCaseLoadingTask> list = basisCaseLoadingTaskService.selectBasisCaseLoadingTaskList(basisCaseLoadingTask);
        ExcelUtil<BasisCaseLoadingTask> util = new ExcelUtil<BasisCaseLoadingTask>(BasisCaseLoadingTask.class);
        util.exportExcel(response, list, "案例关联的任务数据");
    }

    /**
     * 获取案例关联的任务详细信息
     */
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId)
    {
        return success(basisCaseLoadingTaskService.selectBasisCaseLoadingTaskByTaskId(taskId));
    }

    /**
     * 新增案例关联的任务
     */
    @Log(title = "案例关联的任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasisCaseLoadingTask basisCaseLoadingTask)
    {
        int i = basisCaseLoadingTaskService.insertBasisCaseLoadingTask(basisCaseLoadingTask);


        if (i > 0){
            if (!StringUtils.isEmpty(basisCaseLoadingTask.getUavTableIds())) {
                String[] split = basisCaseLoadingTask.getUavTableIds().split(",");
                for (String s : split) {

                    BaseSignalanalysisInfo baseSignalanalysisInfo = new BaseSignalanalysisInfo();

                    BasisUavTable basisUavTable = basisUavTableService.selectBasisUavTableById(Long.valueOf(s));
                    baseSignalanalysisInfo.setCaseId(basisCaseLoadingTask.getLoadingId());
                    baseSignalanalysisInfo.setCaseName(basisCaseLoadingTask.getLoadingName());
                    baseSignalanalysisInfo.setTaskId(basisCaseLoadingTask.getTaskId());
                    baseSignalanalysisInfo.setTaskName(basisCaseLoadingTask.getTaskName());

                    baseSignalanalysisInfo.setUserId(Long.valueOf(basisCaseLoadingTask.getUid()));
                    baseSignalanalysisInfo.setTimeslot(basisUavTable.getTimeslot());

                    baseSignalanalysisInfoService.insertBaseSignalanalysisInfo(baseSignalanalysisInfo);

                }
            }

        }


        return toAjax(i);
    }

    /**
     * 修改案例关联的任务
     */
    @PostMapping("/updateTable")
    public AjaxResult edit(@RequestBody BasisCaseLoadingTask basisCaseLoadingTask)
    {
        return toAjax(basisCaseLoadingTaskService.updateBasisCaseLoadingTask(basisCaseLoadingTask));
    }

    /**
     * 删除案例关联的任务
     */
    @Log(title = "案例关联的任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable String[] taskIds)
    {
        return toAjax(basisCaseLoadingTaskService.deleteBasisCaseLoadingTaskByTaskIds(taskIds));
    }

    @PostMapping("/queryCaseList")
    public AjaxResult queryCaseList()
    {
        Long userid = getUserId();
        return success(basisCaseLoadingTaskService.queryCaseList(userid));
    }


    @GetMapping("/queryTaskList/{caseID}")
    public AjaxResult queryTaskList(@PathVariable("caseID") String caseID)
    {
        BasisCaseLoadingTask basisCaseLoadingTask = new BasisCaseLoadingTask();
        basisCaseLoadingTask.setLoadingId(caseID);
        basisCaseLoadingTask.setUid(getUserId().toString());
        List<BasisCaseLoadingTask> list = basisCaseLoadingTaskService.queryTaskList(basisCaseLoadingTask);
        return success(list);
    }

    @PostMapping("/queryCaseListForS")
    public AjaxResult queryCaseListForS()
    {
        Long userid = getUserId();
        return success(basisCaseLoadingTaskService.queryCaseListForS(userid));
    }


    @GetMapping("/queryTaskListForS/{caseID}")
    public AjaxResult queryTaskListForS(@PathVariable("caseID") String caseID)
    {
        if("1".equals(caseID)){
            caseID = "";
        }
        BasisCaseLoadingTask basisCaseLoadingTask = new BasisCaseLoadingTask();
        basisCaseLoadingTask.setLoadingId(caseID);
        basisCaseLoadingTask.setUid(getUserId().toString());
        List<BasisCaseLoadingTask> list = basisCaseLoadingTaskService.queryTaskListForS(basisCaseLoadingTask);
        return success(list);
    }

    //下发任务
    @GetMapping("/issuedTask")
    public AjaxResult queryTaskList(String loadingId, String taskId)
    {

        return success(basisCaseLoadingTaskService.issuedTask(loadingId, taskId));
    }

    @PostMapping("/updatePush")
    public R updateByPushId(@Param("taskId") String taskId){
        return basisCaseLoadingTaskService.updateByPushId(taskId);
    }

    @GetMapping("/queryUTaskList")
    public AjaxResult queryUTaskList(){
        Long userid = getUserId();
        return success(basisCaseLoadingTaskService.queryUTaskList(userid));
    }



    //根据案例id和人员id查询 任务当前操作流程
    @GetMapping("/findByLoadingIdAndUserIdFormloadingTaskCurrectStatus")
    public AjaxResult findByLoadingIdAndUserIdFormloadingTaskCurrectStatus(String loadingId,Long userId) {
        return AjaxResult.success(basisCaseLoadingTaskService.findByLoadingIdAndUserIdFormloadingTaskCurrectStatus(loadingId,userId));
    }


    @GetMapping("/updateUserTaskStatus")
    public AjaxResult updateUserTaskStatus(String chooseCaseId,String taskId, int newValue){
        Long userid = getUserId();

        return success(basisCaseLoadingTaskService.updateUserTaskStatus(chooseCaseId,taskId,newValue,userid));
    }


}
